package org.zzz.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.zzz.entity.Course;
import org.zzz.entity.TeacherCourse;
import org.zzz.mapper.CourseMapper;
import org.zzz.mapper.TeacherCourseMapper;
import org.zzz.service.TeacherCourseService;
import org.zzz.utils.MybatisUtil;

import java.util.List;

/**
 * @Author ZZF
 * @Time 2021/04/20 22:47
 */
public class TeacherCourseServiceImpl implements TeacherCourseService {

    private TeacherCourseMapper teacherCourseMapper;
    private CourseMapper courseMapper;

    {
//        SqlSession sqlSession = MybatisUtil.getSqlSession();
        teacherCourseMapper = MybatisUtil.getMapper(TeacherCourseMapper.class);
        courseMapper = MybatisUtil.getMapper(CourseMapper.class);
    }

    @Override
    public boolean insertTC(TeacherCourse teacherCourse) {
        return teacherCourseMapper.insertTC(teacherCourse) > 0;
    }

    @Override
    public boolean updateTC(TeacherCourse teacherCourse) {
        return teacherCourseMapper.updateTC(teacherCourse) > 0;
    }

    @Override
    public boolean deleteTCById(String id) {
        return teacherCourseMapper.deleteTCById(id) > 0;
    }

    @Override
    public String getCourseNamesByTid(String tid) {
        List<String> names = teacherCourseMapper.listCourseNameByTid(tid);
        StringBuffer stringBuffer = new StringBuffer();
        String courseNames = "";
        for (String name : names) {
            stringBuffer.append(name).append(", ");
        }
        if (stringBuffer.length() != 0) {
            courseNames = stringBuffer.toString().substring(0,stringBuffer.length() -  2);
        }
        return courseNames;
    }

    @Override
    public String getCourseNameByTid(String tid) {
        TeacherCourse tc = teacherCourseMapper.getTCByTid(tid);
        String courseName = "";
        if (tc != null){
            String cid = tc.getCid();
            Course course = courseMapper.getCourseById(cid);
            courseName = course.getName();
        }
        return courseName;
    }

    @Override
    public TeacherCourse getTCByCid(String cid) {
        return teacherCourseMapper.getTCByCid(cid);
    }

    @Override
    public TeacherCourse getTCByTid(String tid) {
        return teacherCourseMapper.getTCByTid(tid);
    }

//    @Override
//    public TeacherCourse getTCByTid(String tid) {
//        return null;
//    }
}
